﻿AB.namespace("INV_ShortageOrder.Mgr");
INV_ShortageOrder.Mgr = {
    commandHandler: function (command, record) {
        var FId = record.data.FId;
        switch (command) {
            case "Edit":
                FIdH.setValue(FId);
                ShortageOrderDetailStore.reload();
                break;
            case "View":
                FIdH.setValue(FId);
                ShortageOrderDetailStore.reload();
                break;
            case "Close":
                Ext.Msg.confirm('提示', '确定要删除关闭该欠货单吗?关闭后将无法重新打开该欠货单', function (btn) {
                    if (btn == "yes") {
                        Ext.Ajax.request({
                            url: "/ShortageOrder/CloseShortageOrder",
                            method: 'post',
                            params: {
                                FId: FId
                            },
                            success: function (result) {
                                var response = Ext.util.JSON.decode(result.responseText);
                                if (!isTimeOut(result)) {
                                    if (response.success == false) {
                                        ajaxFailureHandler(response);
                                    } else {
                                        ShortageOrderListStore.reload();
                                        alert("关闭欠货单成功！");
                                    }
                                }
                            }
                        });
                    }
                });
                break;
            case "Print":
                window.open("/ShortageOrder/ExportShortageOrderToExcel?FId=" + FId);
                break;
        }
    },
    orderDetailCommandHandler: function (command, record) {
        switch (command) {
            case "SaveOrder":
                if (record.data.FReturnNumber == "" || record.data.FReturnNumber == "undefined") {
                    Ext.Msg.show({
                        title: '操作失败',
                        msg: "请输入归还数量后再保存！",
                        buttons: Ext.Msg.OK,
                        icon: Ext.Msg.ERROR
                    });
                    return;
                }
                if (new Number(record.data.FShortageNumber) < new Number(record.data.FReturnNumber)) {
                    Ext.Msg.show({
                        title: '操作失败',
                        msg: "归还数量不能大于欠货数量，请修改后再保存！",
                        buttons: Ext.Msg.OK,
                        icon: Ext.Msg.ERROR
                    });
                    return;
                }
                Ext.Ajax.request({
                    url: "/ShortageOrder/UpdateShortageOrderDetail",
                    method: 'post',
                    params: {
                        fId: record.data.FId,
                        fParentId: record.data.FParentId,
                        returnNum: record.data.FReturnNumber

                    },
                    success: function (result) {
                        if (!isTimeOut(result)) {
                            var response = Ext.util.JSON.decode(result.responseText);
                            if (response.success == false) {
                                ajaxFailureHandler(response);
                            } else {
                                alert("修改成功！");
                                ShortageOrderListStore.reload();
                                FIdH.setValue(response.extraParamsResponse.FId);
                                ShortageOrderDetailStore.reload();
                            }
                        }
                    }
                });
                break;
        }
    },
    searchList: function () {
        ShortageOrderListStore.reload();
    },

    resetSearchFrom: function () {
        var formObj = Ext.getCmp("SearchFormPanel");
        formObj.reset();
    }
}